package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.*;
import com.itheima.service.UserService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.ws.rs.PUT;
import java.util.*;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        if (user == null) {
            return null;
        }
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findByUserId(userId);
        if (roles != null && roles.size() > 0) {
            for (Role role : roles) {
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                if (permissions != null && permissions.size() > 0) {
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }
        return user;
    }

    //分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();

        PageHelper.startPage(currentPage, pageSize);
        Page<User> page = userDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(User user, Integer[] roleIds) throws Exception {
        userDao.add(user);
        setUserAndRole(user.getId(), roleIds);
        System.out.println(user.getId());
    }

    public User findById(Integer id) {
        return userDao.findById(id);
    }

    public List<Integer> findRoleIdsByUserId(Integer id) {
        return userDao.findRoleIdsByUserId(id);
    }

    public void edit(User user, Integer[] roleIds) {
        userDao.deleteAssocication(user.getId());
        setUserAndRole(user.getId(),roleIds);
        userDao.edit(user);
    }

    @Override
    public void delete(Integer id) {
        userDao.deleteRelation(id);
        userDao.delete(id);
    }

    @Override
    public List<Menu> findMenuByUsername(String username){
        //找的是所有一级菜单
        List<Menu> menu = userDao.findMenuByUsername(username);

        for (Menu menu1 : menu) {
            //获取父IDfindMenu
            Integer id = menu1.getId();
            //传入用户对应的父ID的子菜单集合
            List<Menu> children = userDao.findMenu(id,username);
            //添加给父ID
            menu1.setChildren(children);
        }
        return menu;
    }



    private void setUserAndRole(Integer userId, Integer[] roleIds) {
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("user_id", userId);
                map.put("role_id", roleId);
                userDao.setUserAndRole(map);
            }
        }
    }
}

